package com.scau.clothes.sale.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scau.clothes.sale.model.entity.Sale;
import com.scau.clothes.sale.model.entity.YearSales;
import com.scau.clothes.sale.model.entity.nameSale;
import com.scau.clothes.sale.model.vo.PieDataVo;
import com.scau.clothes.sale.mapper.SaleClothesMapper;
import com.scau.clothes.sale.mapper.SaleMapper;
import com.scau.clothes.sale.model.entity.SaleClothes;
import com.scau.clothes.sale.service.SaleClothesService;
import com.scau.clothes.stock.mapper.ClothesMapper;
import com.scau.clothes.stock.mapper.ClothesNormMapper;
import com.scau.clothes.stock.model.dto.ClothesDto;
import com.scau.clothes.stock.model.dto.ClothesNormDto;
import com.scau.clothes.stock.model.entity.Clothes;
import com.scau.clothes.stock.model.entity.ClothesNorm;
import jakarta.annotation.Resource;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SaleClothesServiceImpl extends ServiceImpl<SaleClothesMapper, SaleClothes> implements SaleClothesService {
    @Resource
    private SaleClothesMapper saleClothesMapper;
    @Resource
    private ClothesNormMapper clothesNormMapper;


    @Override
    public int updateSaleClothes(SaleClothes saleClothes) {
        return saleClothesMapper.updateById(saleClothes);
    }

    @Override
    public int deleteSaleClothes(SaleClothes saleClothes) {
        saleClothes.setIsDelete(1l);
        return saleClothesMapper.updateById(saleClothes);
    }



    @Override
    public List<nameSale> selectSaleItemList(String startDate, String endDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 创建DateTimeFormatter对象并指定日期格式

        LocalDate Startdate = null;
        LocalDate Enddate = null;

        try {
            Startdate = LocalDate.parse(startDate, formatter);
             Enddate = LocalDate.parse(endDate, formatter);

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(Startdate);
        System.out.println(Enddate);

        return saleClothesMapper.selectSaleClothes(Startdate, Enddate);
    }

    @Override
    public Map<Integer, BigDecimal> getYearSales(int year) {
        List<YearSales> list = saleClothesMapper.selectYearSales(year);
        Map<Integer, BigDecimal> map = list.stream().collect(Collectors.toMap(YearSales::getMonth, YearSales::getTotal));
        for (int i = 1; i <= 12; i++) {
            map.putIfAbsent(i, new BigDecimal(0));
        }
        return map;
    }




}
